package com.yxjx.duoxue.datamodel;

import org.json.JSONException;
import org.json.JSONObject;

import com.yxjx.duoxue.utils.UIHelper;

/**
 * Avoid null point check.
 * @author mengmengchen
 *
 */
public class DMObject {
	/**
	 * 
	 * @param src
	 * @return
	 */
	private static String castStringInternal(Object src) {
		if (src == null) {
			return "";
		} else if (src instanceof String) {
			return (String) src;
		} else {
			return src.toString();
		}
	}
	
	/**
	 * 
	 * @param src
	 * @return
	 */
	private static Integer castIntegerInternal(Object src) {
		if (src == null) {
			return -1;
		} else if (src instanceof Integer) {
			return (Integer) src;
		} else if (src instanceof String) {
			try {
				int intValue = Integer.valueOf((String) src);
				return intValue;
			} catch (Exception e) {
				//
			}
 		}
		
		return -1;
	}
	
	/**
	 * 
	 * @param src
	 * @return
	 */
	private static Double castDoubleInternal(Object src) {
		if (src == null) {
			return -1d;
		} else if (src instanceof Double) {
			return (Double) src;
		} else if (src instanceof String) { 
			try {
				Double value = Double.valueOf((String) src);
				return value;
			} catch (Exception e) {
				//
			}
			return -1d;
		} else if (src instanceof Integer) {
			return (double) castIntegerInternal(src);
		} 
		
		return -1d;
	}
	
	/**
	 * 
	 * @param src
	 * @return
	 */
	private static Long castLongInternal(Object src) {
		if (src == null) {
			return -1l;
		} else if (src instanceof Long) {
			return (Long) src;
		} else if (src instanceof String) { 
			try {
				Long value = Long.valueOf((String) src);
				return value;
			} catch (Exception e) {

			}
		} else if (src instanceof Integer) {
			return (long) castIntegerInternal(src);
		} 
		return -1l;
	}
	
	// 少用
	public static Long castLong(Object src) {
		return castLongInternal(src);
	}
	
	// 上面的这些api在和JSONObject get方法一起使用时，可能会有no such key的exception。
	/**
	 * getLong
	 * @param jo
	 * @param key
	 * @return
	 */
	public static Long getLong(JSONObject jo, String key) {
		
		if (jo == null) {
			return castLongInternal(null);
		}
		
		try {
			return castLongInternal(jo.get(key));
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return castLongInternal(null);
	}
	
	/**
	 * getString
	 * @param jo
	 * @param key
	 * @return
	 */
	public static String getString(JSONObject jo, String key) {
		if (jo == null) {
			return castStringInternal(null);
		}
		
		try {
			return castStringInternal(jo.get(key));
		} catch (JSONException e) {
			
		}
		return castStringInternal(null);
	}
	
	/**
	 * getInteger
	 * @param jo
	 * @param key
	 * @return
	 */
	public static Integer getInteger(JSONObject jo, String key) {
		if (jo == null) {
			return castIntegerInternal(null);
		}

		try {
			return castIntegerInternal(jo.get(key));
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
		return castIntegerInternal(null);
	}
	
	/**
	 * getDouble
	 * @param jo
	 * @param key
	 * @return
	 */
	public static Double getDouble(JSONObject jo, String key) {
		if (jo == null) {
			return castDoubleInternal(null);
		}
		
		try {
			return castDoubleInternal(jo.get(key));
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
		return castDoubleInternal(null);
	}
	
	// new API
	/**
	 * 
	 * @param src
	 * @return
	 */
	public static String castString(JSONObject jo, String key) {
		return getString(jo, key);
	}
	
	/**
	 * 
	 * @param src
	 * @return
	 */
	public static Integer castInteger(JSONObject jo, String key) {
		return getInteger(jo, key);
	}
	
	/**
	 * 
	 * @param src
	 * @return
	 */
	public static Double castDouble(JSONObject jo, String key) {
		return getDouble(jo, key);
	}
	
	/**
	 * 
	 * @param src
	 * @return
	 */
	public static Long castLong(JSONObject jo, String key) {
		return getLong(jo, key);
	}
	
	public static JSONObject getJSONObject(JSONObject jo, String key) {
		if (jo == null || UIHelper.isEmpty(key)) {
			return null;
		}
		
		try {
			Object ret = jo.get(key);
			if (ret instanceof JSONObject) {
				return (JSONObject) ret;
			} else if (ret instanceof String) {
				JSONObject retJo = new JSONObject((String)ret);
				return retJo;
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return null;
		
	}
	
}
